package by.grsu.coctails.fragments;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import by.grsu.coctails.R;
import by.grsu.coctails.adapter.AbstractAdapter;
import by.grsu.coctails.adapter.CoctailAdapter;
import by.grsu.coctails.common.utils.ImageManager;
import by.grsu.coctails.constants.ApplicationIntents;
import by.grsu.coctails.db.task.CommonGetAsyncTask;
import by.grsu.coctails.db.task.GetCoctailWithIngredientAsyncTask;
import by.grsu.coctails.model.Coctail;
import by.grsu.coctails.model.Ingredient;

public class IngredientFragment extends CommonDBFragment<Coctail> {

	public static final String TAG = IngredientFragment.class.getSimpleName();

	private static final String INGREDIENT_KEY = "INGREDIENT_KEY";

	private Ingredient mIngredient;

	private ImageView mAddImageView;

	private LinearLayout mAddButton;

	public static IngredientFragment newInstance(Ingredient ingredient) {
		Bundle bundle = new Bundle();
		bundle.putParcelable(INGREDIENT_KEY, ingredient);

		IngredientFragment fragment = new IngredientFragment();
		fragment.setArguments(bundle);

		return fragment;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		mIngredient = getArguments().getParcelable(INGREDIENT_KEY);
		mAddImageView = (ImageView) getView().findViewById(R.id.add_image_view);
		mAddButton = (LinearLayout) getView().findViewById(R.id.add_button);

		initAddButton();
		ImageView image = (ImageView) getView().findViewById(R.id.image);
		getImageLoader().bind(image,
				ImageManager.HTTP + mIngredient.getImage(), null);

		TextView title = (TextView) getView().findViewById(R.id.title);
		title.setText(mIngredient.getName());

		getListView().setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					int position, long arg3) {
				Coctail coctail = getAdapter().getItem(position);
				FragmentCreator.addCompostionFragment(getActivity(), coctail);
			}
		});

		getTask("").start();

	}

	private void initAddButton() {
		updateAddButton();
		mAddButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				onAddButtonClick();
			}
		});

	}

	private void updateAddButton() {
		if (mIngredient.isHaved() != 1) {
			mAddImageView.setImageResource(R.drawable.ic_action_new);
		} else {
			mAddImageView.setImageResource(R.drawable.ic_action_delete);
		}
	}

	private void onAddButtonClick() {
		int isHaved = mIngredient.isHaved();
		isHaved = mIngredient.isHaved() == 1 ? 0 : 1;
		if (getDbManager().updateMyIngredient(mIngredient.getName(), isHaved) <= 0) {
			Toast.makeText(getActivity(), R.string.error_when_update_database,
					Toast.LENGTH_SHORT).show();
		} else {
			mIngredient.setHaved(isHaved);
			updateAddButton();
			String toastText = mIngredient.getName() + " ";
			toastText += mIngredient.isHaved() == 1 ? getString(R.string.success_added)
					: getString(R.string.success_deleted);

			Intent intent = new Intent(
					ApplicationIntents.ON_CHANDGE_INGREDIENT_INTENT);

			intent.putExtra(ApplicationIntents.INGREDIENT_NAME_KEY,
					mIngredient.getName());
			intent.putExtra(ApplicationIntents.INGREDIENT_STATE_KEY,
					mIngredient.isHaved());
			getActivity().sendBroadcast(intent);

			Toast.makeText(getActivity(), toastText, Toast.LENGTH_SHORT).show();

		}
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public CommonGetAsyncTask getTask(String categoryName) {
		return new GetCoctailWithIngredientAsyncTask(getActivity(),
				mIngredient.getName()) {

			@Override
			public void onComplete(List<Coctail> result) {
				getAdapter().getList().addAll(result);
				getAdapter().notifyDataSetChanged();

			}
		};
	}

	@Override
	public int getLayoutResource() {
		return R.layout.ingredient_fragment;
	}

	@Override
	public AbstractAdapter<Coctail> createAdapter() {
		List<Coctail> list = new ArrayList<Coctail>();
		return new CoctailAdapter(getActivity(), R.layout.coctail_item, list);
	}

	@Override
	public String getTitle() {
		return null;
	}

}
